package com.lbx.algo.sort;

import java.util.Arrays;

/**
 * 插入排序
 */
public class InsertSort {

    public static  void insertSort(int[] nums,int n){
     if(n <= 1){
         return;
     }
     for(int i= 1;i< n;i++){
         int value = nums[i];
         int j = i-1;
         for(;j >= 0;--j){
             System.out.println("j="+j+",nums[j]= "+nums[j]+","+value);
             if(nums[j] > value){
                 nums[j+1] = nums[j];
             }else{
                 break;
             }
         }
         nums[j + 1] = value;
         System.out.println(Arrays.toString(nums));
     }
    }

    public static void main(String[] args) {
        int[] array = new int[]{9,3, 4, 2, 1, 51, 6, 17, 8};
        insertSort(array, array.length);
        System.out.println(Arrays.toString(array));
    }
}
